<!DOCTYPE html>
<html>
<head><title>节点事件列表配置</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" id="bpm_theme" type="text/css" href="../../easyui/newtheme/bule/devclient.css">
    <script type="text/javascript" src="../../jscode/lang/zh_CN.js"></script>
    <script type="text/javascript" src="../../easyui/jquery.min.js"></script>
    <script type="text/javascript" src="../../easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="../../jscode/sharefunction.js"></script>
    <script type="text/javascript" src="../../jscode/json.js"></script>
    <script type="text/javascript" src="../../jscode/viewgrid.js"></script>
    <script type="text/javascript" src="../../easyui/locale/easyui-lang-zh_CN.js"></script>

    <script>

        var CONFIG = [];
        $.ajax({
            url: "../config/config.json",
            //同步请求
            async: false,
            type: 'GET',
            success: function (data) {
                CONFIG = data;
            }
        });

        function formatlink(v, r) {
            return "<a href='' onclick=\"RowDblClick(0,'" + r.WF_OrUnid + "');return false;\" >" + v + "</a>";
        }

        function GroupFormat(value, rows) {
            return value + ' - ' + rows.length + ' 项';
        }

        $(window).resize(function () {
            $('#dg').datagrid('resize', {
                width: function () {
                    return document.body.clientWidth;
                }, height: function () {
                    return document.body.clientHeight;
                }
            });
        });

        $(function () {

            var processAttrUrl = CONFIG.url.getFlowChartEventByNodeType;
            processAttrUrl = processAttrUrl.replace("{processid}", GetUrlArg("Processid")).replace("{nodeid}", GetUrlArg("Nodeid"));

            $('#dg').datagrid({
                method: 'GET',
                url: processAttrUrl,
            });
            // $.getJSON(processAttrUrl,
            //     function(data){
            //         $('#dg').datagrid('loadData', data);
            //     });

        })

        /**
         * 获取规则分类树
         **/
        function getTreeData(){
            var treeData = null;

            $.ajax({
                type : 'GET',
                url : CONFIG.url.getRuleTree, // 得到数据的URL地址
                cache : false,
                async : false,
                dataType : 'json',
                data : {}, // 请求参数
                success : function(result) {
                    treeData = result;
                }
            });
            return treeData;

        }

        /**
         * 获取事件类型
         *  url:'../json/ProcessEvent.json',
         **/
        function getEventData(){
            var treeData = null;

            var eventType = GetUrlArg("eventType");
            $.ajax({
                type : 'GET',
                url : '../json/' + eventType + '.json', // 得到数据的URL地址
                cache : false,
                async : false,
                dataType : 'json',
                data : {}, // 请求参数
                success : function(result) {
                    treeData = result;
                }
            });
            return treeData;

        }


        /**
         * 保存提交
         * */
        function saveEditorGrid() {
            //获得所有修改的数据
            stopEditRow();
            var dg = $("#dg");

            //获得所有修改的数据
            var delRow = JSON.stringify(dg.datagrid('getChanges', 'deleted'));
            console.dir(GetUrlArg("Nodeid"))
            var rowData = dg.datagrid('getChanges', 'inserted');
            for (var i = 0; i < rowData.length; i++) {
                rowData[i].Processid = GetUrlArg("Processid");
                rowData[i].Nodeid = GetUrlArg("Nodeid");
            }
            // console.dir(rowData)
            var newRow = JSON.stringify(rowData);
            var editRow = JSON.stringify(dg.datagrid('getChanges', 'updated'));


            var saveFlowAttrUrl = CONFIG.url.saveFlowChartEventByNodeType;
            saveFlowAttrUrl = saveFlowAttrUrl.replace("{processid}", GetUrlArg("Processid")).replace("{nodeid}", GetUrlArg("Nodeid"));


            //保存修改结果
            mask();
            $.post(saveFlowAttrUrl, {
                WF_Action: 'saveEditor',
                delRow: delRow,
                newRow: newRow,
                editRow: editRow
            }, function (data) {
                if (data.Status == "error") {
                    alert(data.msg);
                } else {
                    dg.datagrid('acceptChanges');
                }
                unmask();
            });
        }

        function serializeForm() {
        }

        function insertRow() {
            //插入新行
            stopEditRow();
            var i = $('#dg').datagrid('getData').rows.length;
            $('#dg').datagrid('insertRow', {index: i, row: {}});
        }

        function removeSelectedRow() {
            //删除选中行
            stopEditRow();
            var rows = $('#dg').datagrid('getSelections');
            if (!rows || rows.length == 0) {
                $.messager.alert('提示', '请选择要删除的数据!', 'info');
                return;
            }
            $.messager.confirm('Confirm', '您确定要删除选中记录吗?', function (r) {
                if (r) {
                    var dg = $('#dg');
                    $.each(rows, function (i, row) {
                        var index = dg.datagrid('getRowIndex', row);
                        dg.datagrid('deleteRow', index);
                    });
                }
            });
        }

        var lastIndex;

        function RowClick(rowIndex) {
            //单击进行修改
            $("#dg").datagrid('unselectAll');
            $("#dg").datagrid('selectRow', rowIndex);
            if (lastIndex != rowIndex) {
                $('#dg').datagrid('endEdit', lastIndex);
                $('#dg').datagrid('beginEdit', rowIndex);
            }
            lastIndex = rowIndex;
        }

        function stopEditRow() {
            $('#dg').datagrid('endEdit', lastIndex);
            lastIndex = undefined;
        }

        /**
         * 跳转配置事件规则
         */
        function gotoConfigRule() {
            var url = "./RuleSetting.html?Processid=" + GetUrlArg("Processid") + "&Nodeid=" + GetUrlArg("Nodeid");
            location.href = url;
        }


    </script>
</head>
<html>
<head></head>
<body>
<div class="toptoolbar" id="toptoolbar">
    |
    <a href="#" id="btnSave" class="easyui-linkbutton" plain="true" data-options="iconCls:'icon-save'"
       onclick="saveEditorGrid();return false;">保存事件</a> -
    <a href="#" id="btnNew" class="easyui-linkbutton" plain="true" data-options="iconCls:'icon-add'"
       onclick="insertRow();return false;">新增事件</a> -
    <a href="#" id="btnDel" class="easyui-linkbutton" plain="true" data-options="iconCls:'icon-remove'"
       onclick="removeSelectedRow();return false;">删除</a> -
    <a href="#" id="btn01" class="easyui-linkbutton" plain="true" data-options="iconCls:'icon-ok'"
       onclick="stopEditRow();return false;">停止编辑</a> -
    <a href="#" id="btnRuleConfig" class="easyui-linkbutton" plain="true" data-options="iconCls:'icon-add'"
       onclick="gotoConfigRule();return false;">配置规则</a>
</div>
<table id="dg" class="easyui-datagrid"
       data-options="toolbar:toptoolbar,fit:true,autoRowHeight:false,remoteSort:true,border:false,rownumbers: true,fitColumns:true,singleSelect:false,multiSort:true,onDblClickRow:stopEditRow,onClickRow:RowClick,onRowContextMenu:stopEditRow,pageSize:25,pageList:[20,25,30,40,60]">
    <thead>
    <tr>
        <th data-options="checkbox:true"></th>
        <th data-options="field:'icon',width:30,align:'center',formatter:function(v,r){return '&lt;img src=../../images/icons/event.gif &gt;';}"></th>
        <th id="abc" data-options="field:'Eventid',width:160,sortable:true,editor:{type:'combobox',options:{
                valueField:'Eventid',
                textField:'EventName',
                data: getEventData(),
                formatter:function(row){ var s = row.EventName + '('+row.Eventid+')';return s;}
            }}">对应事件id
        </th>
        <th data-options="field:'RuleNum',width:200,sortable:true,editor:{type:'combotree',options:{
                data: getTreeData()
            }}">触发规则
        </th>
        <th data-options="field:'Params',width:130,sortable:true,editor:'text'">规则参数</th>
        <th data-options="field:'SortNum',width:60,sortable:true,editor:'text'">排序号</th>
    </tr>
    </thead>
</table>
<div id="win"></div>
</body>
</html>
</html>